package shop.maytinh.controller;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.AliasToBeanResultTransformer;
import org.hibernate.transform.Transformers;

import shop.maytinh.util.HibernateUtil;
import shop.maytinh.model.*;

 

public class OderManager extends HibernateUtil {
	
	List<Oder> listOder;
	static List<Comment> listcomment;
	public Comment comment = null; 
	private Oder oder;
	
	public List<Oder> getlistOder(){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		listOder = null;
	    session.beginTransaction();			
	    try{
	    	listOder = (List<Oder>)session.createQuery("from Oder").list();	    	
			if(listOder==null)
				return null;
		}
		catch(Exception e){
			 e.printStackTrace();
			 session.getTransaction().rollback();
		}
	    session.getTransaction().commit();
		return listOder;
	}	
	public int addOder(Oder oder) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(oder);
		session.getTransaction().commit();
		return oder.getId();
	}
	
	public Detail_oder addDetailOder(Detail_oder detailOder) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(detailOder);
		session.getTransaction().commit();
		return detailOder;
	}
	

	public Oder getOderById(Integer id) throws Exception {
		
        try {
        	Session session = HibernateUtil.getSessionFactory().getCurrentSession();    	 
        	session.beginTransaction();
        	Oder oder = null;                                
        	oder = (Oder) session.createCriteria(Oder.class)
                    .add(Restrictions.eq("id",id))
                    .uniqueResult();
        	session.getTransaction().commit();
            return oder;                       
        } catch (Exception e)
        {
            throw e;
        }
        
    }
public Detail_oder getDetailOderById(Integer id) throws Exception {
		
        try {
        	Session session = HibernateUtil.getSessionFactory().getCurrentSession();    	 
        	session.beginTransaction();
        	Detail_oder detailOder = null;                                
        	detailOder = (Detail_oder) session.createCriteria(Detail_oder.class)
                    .add(Restrictions.eq("id",id))
                    .uniqueResult();
        	session.getTransaction().commit();
            return detailOder;                       
        } catch (Exception e)
        {
            throw e;
        }
        
    }

}


